import Mock from "mockjs";
let url = "http://127.0.0.1:8080"
let userinfo = [
  { uname: "tom", upwd: "123456" }, 
  { uname: "toni", upwd: "1234567" },
  { uname: "noki", upwd: "12345678" },
];
//   域名                方法   前端传递参数
// 登录接口
Mock.mock(`${url}/user/login`, "post", (options) => {
  let info = JSON.parse(options.body);
  let i=0
  userinfo.map(item => {
      if(item.uname == info.uname && item.upwd == info.upwd){
        i++
      }
  })
  if(i==0){
      return {
          code:0,
          msg:'用户名或密码错误'
      }
  }else{
      return{
          code:1,
          msg:'登录成功'
      }
  }
});
//注册接口
Mock.mock(`${url}/user/reg`,'post',options => {
    let info = JSON.parse(options.body)
    let i=0
    userinfo.map(item => {
        if(item.uname == info.uname){
            i++
        }
    })
    if(i){
        return{
            code:0,
            msg:'用户名已存在'
        }
    }else{
        userinfo.push(info)
        return{
            code:1,
            data:info,
            msg:'注册成功'
        }
    }
})